package io.renren.modules.bs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.validator.Assert;
import io.renren.modules.app.form.AppLoginForm;
import io.renren.modules.bs.dao.BsDepartmentAdministratorDao;
import io.renren.modules.bs.entity.BsDepartmentAdministratorEntity;
import io.renren.modules.bs.service.BsDepartmentAdministratorService;
import io.renren.modules.bs.vo.DepartmentAdministratorVo;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("bsDepartmentAdministratorService")
public class BsDepartmentAdministratorServiceImpl extends ServiceImpl<BsDepartmentAdministratorDao, BsDepartmentAdministratorEntity> implements BsDepartmentAdministratorService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<BsDepartmentAdministratorEntity> page = this.page(
//                new Query<BsDepartmentAdministratorEntity>().getPage(params),
//                new QueryWrapper<BsDepartmentAdministratorEntity>()
//        );
        Integer total = this.baseMapper.getTotal(params);
        Integer page = Integer.parseInt((String) params.get("page"));
        Integer limit = Integer.parseInt((String) params.get("limit"));;
        params.put("start",(page -1)*limit);
        params.put("page",page);
        params.put("limit",limit);
        List<DepartmentAdministratorVo> pageList = this.baseMapper.getPageList(params);
        return  new PageUtils(pageList,total,page,limit);
    }

    @Override
    public BsDepartmentAdministratorEntity login(AppLoginForm form) {

        BsDepartmentAdministratorEntity admin = this.getByAccount(form.getAdministratorAccount());
        Assert.isNull(admin,"账号不存在");
        //账号不存在、密码错误
        if( !admin.getAdministratorPwd().equals(new Sha256Hash(form.getAdministratorPwd(), admin.getSalt()).toHex())) {
            throw new RRException("账号或密码错误");
        }

        return admin;
    }

    @Override
    public void saveModel(BsDepartmentAdministratorEntity bsDepartmentAdministrator) {

        BsDepartmentAdministratorEntity byAccount = this.getByAccount(bsDepartmentAdministrator.getAdministratorAccount());
        if(byAccount != null){
            throw new RRException("账号已存在");
        }
        String salt = RandomStringUtils.randomAlphanumeric(20);
        bsDepartmentAdministrator.setAdministratorPwd(new Sha256Hash(bsDepartmentAdministrator.getAdministratorPwd(), salt).toHex());
        bsDepartmentAdministrator.setSalt(salt);
        boolean save = this.save(bsDepartmentAdministrator);
        if(!save){
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    public BsDepartmentAdministratorEntity getByAccount(String administratorAccount) {

        QueryWrapper<BsDepartmentAdministratorEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("administrator_account",administratorAccount);
        return getOne(wrapper);
    }

    @Override
    public void updateModel(BsDepartmentAdministratorEntity bsDepartmentAdministrator) {

        BsDepartmentAdministratorEntity administratorEntity = this.getById(bsDepartmentAdministrator.getDepartmentAdministratorId());

        BsDepartmentAdministratorEntity byAccount = this.getByAccount(bsDepartmentAdministrator.getAdministratorAccount());
        if(byAccount != null && administratorEntity.getDepartmentAdministratorId()!= byAccount.getDepartmentAdministratorId()){
            throw new RRException("账号已存在");
        }
        if(StringUtils.isNotBlank(bsDepartmentAdministrator.getAdministratorPwd())){
            String salt = RandomStringUtils.randomAlphanumeric(20);
            bsDepartmentAdministrator.setAdministratorPwd(new Sha256Hash(bsDepartmentAdministrator.getAdministratorPwd(), salt).toHex());
            bsDepartmentAdministrator.setSalt(salt);
        }else {
            bsDepartmentAdministrator.setAdministratorPwd(administratorEntity.getAdministratorPwd());
            bsDepartmentAdministrator.setSalt(administratorEntity.getSalt());
        }

        boolean save = this.updateById(bsDepartmentAdministrator);
        if(!save){
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    public List<BsDepartmentAdministratorEntity> listByDeptIds(List<Long> asList) {

        QueryWrapper<BsDepartmentAdministratorEntity> deptWrapper = new QueryWrapper<>();
        deptWrapper.in("department_id",asList);
        return list(deptWrapper);
    }

    @Override
    public void updatePwd(String newPwd, Long adminId) {

        if (StringUtils.isBlank(newPwd)) {

            throw new RRException("新密码不能为空");
        }

        BsDepartmentAdministratorEntity entity = this.getById(adminId);
        if(entity == null){
            throw new RRException("未找到该管理员");
        }
        String salt = RandomStringUtils.randomAlphanumeric(20);
        entity.setAdministratorPwd(new Sha256Hash(newPwd, salt).toHex());
        entity.setSalt(salt);
        boolean b = this.updateById(entity);
        if(!b){
            throw new RRException("更新密码失败");
        }
    }


}